﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for HomeLoanDB
/// </summary>
/// 
namespace CE.HomeLoan
{
    public class HomeLoanDB
    {
        public HomeLoanDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(HomeLoan homeloan)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocHomeLoanInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@HLId", homeloan.HLId);
            _sPara.Add("@StepId", homeloan.StepId);
            _sPara.Add("@HLMortgageType", homeloan.HLMortgageType);
            _sPara.Add("@HLPropertyType", homeloan.HLPropertyType);
            _sPara.Add("@HLProperty", homeloan.HLProperty);
            _sPara.Add("@HLAmount", homeloan.HLAmount);
            _sPara.Add("@HLPropertyValue", homeloan.HLPropertyValue);
            _sPara.Add("@HLLoanTenure", homeloan.HLLoanTenure);
            _sPara.Add("@HLInterestRate", homeloan.HLInterestRate);
            _sPara.Add("@HLOffsetSchemes", homeloan.HLOffsetSchemes);
            _sPara.Add("@HLLockIn", homeloan.HLLockIn);
            _sPara.Add("@FullName", homeloan.FullName);
            //_sPara.Add("@LastName", homeloan.LastName);
            _sPara.Add("@ContactNo", homeloan.ContactNo);
            _sPara.Add("@Email", homeloan.Email);
            _sPara.Add("@RefCEUserId", homeloan.RefCEUserId);
            _sPara.Add("@ViewedDetailId", homeloan.ViewedDetailId);
            _sPara.Add("@Status", homeloan.Status);

            _sPara.Add("@NInfo1", homeloan.NInfo1);
            _sPara.Add("@NInfo2", homeloan.NInfo2);
            _sPara.Add("@NInfo3", homeloan.NInfo3);
            _sPara.Add("@NInfo4", homeloan.NInfo4);

            _sPara.Add("@Info1", homeloan.Info1);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public Int64 UpdateRequest(HomeLoan homeloan)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocHomeLoanRequestUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@HLId", homeloan.HLId);
            _sPara.Add("@HLAmount", homeloan.HLAmount);
            _sPara.Add("@HLLoanTenure", homeloan.HLLoanTenure);
            _sPara.Add("@HLInterestRate", homeloan.HLInterestRate);
            _sPara.Add("@HLLockIn", homeloan.HLLockIn);
            _sPara.Add("@ViewedDetailId", homeloan.ViewedDetailId);
            _sPara.Add("@Status", homeloan.Status);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public HomeLoan GetHomeLoan(Int64 HLId)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "[sprocHomeLoanSelectSingleItem]";
            HomeLoan _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@HLId", HLId);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new HomeLoan();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private HomeLoan FillDataRecord(DataTable myDataTable)
        {
            HomeLoan myHomeLoan = new HomeLoan();
            myHomeLoan.HLId = Convert.ToInt64(myDataTable.Rows[0]["HLId"].ToString());
            myHomeLoan.HLMortgageType = Convert.ToInt16(myDataTable.Rows[0]["HLMortgageType"].ToString());
            myHomeLoan.HLPropertyType = Convert.ToInt16(myDataTable.Rows[0]["HLPropertyType"].ToString());
            myHomeLoan.HLProperty = Convert.ToInt16(myDataTable.Rows[0]["HLProperty"].ToString());
            myHomeLoan.HLAmount = Convert.ToSingle(myDataTable.Rows[0]["HLAmount"].ToString());
            myHomeLoan.HLPropertyValue = Convert.ToSingle(myDataTable.Rows[0]["HLPropertyValue"].ToString());
            myHomeLoan.HLLoanTenure = Convert.ToInt16(myDataTable.Rows[0]["HLLoanTenure"].ToString());
            myHomeLoan.HLInterestRate = Convert.ToInt16(myDataTable.Rows[0]["HLInterestRate"].ToString());
            myHomeLoan.HLOffsetSchemes = Convert.ToBoolean(myDataTable.Rows[0]["HLOffsetSchemes"].ToString());
            myHomeLoan.HLLockIn = Convert.ToBoolean(myDataTable.Rows[0]["HLLockIn"].ToString());
            myHomeLoan.FullName = myDataTable.Rows[0]["FullName"].ToString();
            //myHomeLoan.LastName = myDataTable.Rows[0]["LastName"].ToString();
            myHomeLoan.ContactNo = myDataTable.Rows[0]["ContactNo"].ToString();
            myHomeLoan.Email = myDataTable.Rows[0]["Email"].ToString();
            myHomeLoan.StepId = Convert.ToInt16(myDataTable.Rows[0]["StepId"].ToString());
            myHomeLoan.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            myHomeLoan.CreatedDate = Convert.ToDateTime(myDataTable.Rows[0]["CreatedDate"].ToString());
            myHomeLoan.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[0]["ModifiedDate"].ToString());
            if (myDataTable.Rows[0]["NInfo1"] != null)
                myHomeLoan.NInfo1 = Convert.ToInt64(myDataTable.Rows[0]["NInfo1"].ToString());

            if (myDataTable.Rows[0]["NInfo2"] != null)
                myHomeLoan.NInfo2 = Convert.ToInt64(myDataTable.Rows[0]["NInfo2"].ToString());

            if (myDataTable.Rows[0]["NInfo3"] != null)
                myHomeLoan.NInfo3 = Convert.ToInt64(myDataTable.Rows[0]["NInfo3"].ToString());

            if (myDataTable.Rows[0]["NInfo4"] != null)
                myHomeLoan.NInfo4 = Convert.ToInt64(myDataTable.Rows[0]["NInfo4"].ToString());

            myHomeLoan.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            return myHomeLoan;
        }

    }
}